package com.opgea.crm.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.vo.ContactVo;
import com.opgea.crm.common.vo.LeadVo;
import com.opgea.crm.domain.entities.Lead;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.AdditionalContactService;
import com.opgea.crm.services.EmployeeService;
import com.opgea.crm.services.LeadService;
import com.opgea.crm.services.SetupService;
import com.opgea.crm.web.form.LeadForm;

@Controller

@RequestMapping(value="searchleads")
@SessionAttributes("selectedLeadId")
public class LeadSearchController {

	Logger logger=Logger.getLogger(LeadSearchController.class);
	@Autowired
	private LeadService leadService;
	@Autowired
	SetupService setupService; 
	@Autowired
	EmployeeService employeeService;
	@Autowired
	private AdditionalContactService contactService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView show(HttpServletRequest request){
		return new ModelAndView("searchleads");
	}

	@RequestMapping(method=RequestMethod.GET, value="goToLeadDetails")
	public String goToLeadDetails(){
		return "lead";	
	}	
	
	@RequestMapping(value="showLeads",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showLeads(@RequestParam(required=false) String criteria,
								 @RequestParam(required=false) String value){
		
		logger.debug("SearchLead >> showLeads");
		if(criteria != null && value != null){
			List<LeadVo> list = leadService.findDynamically(criteria, value);
			return JsonModelMap.success().data(list);
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(value="showLeadCustomer",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showLeadCustomer(@RequestParam(required=false) String criteria,
								 @RequestParam(required=false) String value){
		
		logger.debug("SearchLead >> showLeadCustomer");
		System.out.println("SearchLead >> showLeadCustomer");
		if(criteria != null && value != null){
			List<LeadVo> list = leadService.findAllLeadCustomerDynamically(criteria, value);
			return JsonModelMap.success().data(list);
		}else{
			return JsonModelMap.success();
		}
	}
	
	@RequestMapping(value="showContacts",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showContacts(@RequestParam(required=false) String leadId)
	{
		logger.debug("SearchLead >> showContacts");
		if(leadId != null){
			List<ContactVo> list = contactService.findAllByLeadId(new Long(leadId));
			return JsonModelMap.success().data(list);	
		}else{
			return JsonModelMap.success();
		}
		
	}	
	
	//These method is use for lead dashboard to genrate lead details
	@RequestMapping(value="showLeadDetails",method=RequestMethod.GET)
	public @ResponseBody
	Map<String,Object> showLeadDetails(@RequestParam(required=false, value="assignedToId") Long assignedToId,
									   @RequestParam(required=false, value="gradeId") Long gradeId,
									   @RequestParam(required=false, value="stageId") Long stageId){
		
		logger.debug("SearchLead >> find lead according to sales person");
		if(assignedToId == null && gradeId == null && stageId == null){
			return JsonModelMap.success();
		}
		else{
			List<LeadVo> list = leadService.findLeadList(assignedToId,gradeId,stageId);
			return JsonModelMap.success().data(list);
		}
	}
	
	 @RequestMapping(method=RequestMethod.GET, value="stageDashboardTypes")
	 public @ResponseBody Map<String, Object> stageDashboardTypes()
	 {
		 List<SetupsValue> stageList = setupService.listSetupValue(SetupsModuleQualifier.Lead.ordinal(),SetupsAttributeQualifier.Stage.ordinal());
		 stageList.add(new SetupsValue(0l, "All"));
		 return JsonModelMap.success().data(stageList);
	 }
	 
	 @RequestMapping(method=RequestMethod.GET, value="gradeDashboardTypes")
	 public @ResponseBody
	 Map<String, Object> gradeDashboardTypes()
	 {
		 List<SetupsValue> gradeList = setupService.listSetupValue(SetupsModuleQualifier.Lead.ordinal(), SetupsAttributeQualifier.Grade.ordinal());
		 gradeList.add(new SetupsValue(0l, "All"));
		 return JsonModelMap.success().data(gradeList);
	 }
	 
	 @RequestMapping(method=RequestMethod.GET, value="showLeadDashBoardDetails")
	 public @ResponseBody
	 Map<String, Object> showLeadDashBoardDetails(@RequestParam(required= true, value="leadDashboardId") Long leadDashboardId)
	 {
		 Lead lead = leadService.findLead(leadDashboardId);
		 logger.debug("Lead DashBoard : >>> "+lead);
		 return JsonModelMap.success().data(new LeadForm(lead));
	 }
	 
	 @RequestMapping(method=RequestMethod.GET, value="salespersonTypes")
	    public @ResponseBody 
	    Map<String,List<SetupsValue>> salespersonTypes(){
		    List<SetupsValue>  leadList = employeeService.findallEmployeeNameByLeadId();
		    Map<String,List<SetupsValue>> list=new HashMap<String, List<SetupsValue>>();
		    list.put("data",leadList);
		    return list;  //JsonModelMap.success().data(leadList);
	    } 
	 
}
